package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> query(UserDto userDto) {
        //分页
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        //查询
        List<User> users = userMapper.selectByCondition(userDto);
        //解析对象，返回结果
        Page<User> page = (Page<User>) users;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void add(User user) {

        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        String password = user.getUsername() +"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.insert(user);
    }

    @Override
    public void delete(List<Integer> ids) {
        userMapper.deleteByid(ids);

    }

    /**
     * 根据角色ID查询角色
     */
    @Override
    public User selectById(Integer id) {
        return userMapper.select(id);
    }

    /**
     * 删除用户
     */
    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        String password = user.getUsername() +"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.update(user);
    }

    /**
     * 查询所有用户
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    /**
     * 根据角色标识查询用户
     */
    @Override
    public List<User> queryByRole(String roleLabel) {
        return userMapper.selectByRole(roleLabel);
    }
    /**
     * 根据部门查询角色
     */
    @Override
    public List<User> queryByDept(Integer deptId) {
        return userMapper.selectByDept(deptId);
    }
    /**
     * 登入校验
     */
    @Override
    public LoginResultVo login(User user) {
        // 根据用户名查询用户
        User dbUser = userMapper.selectByUsername(user.getUsername());
        // 判断用户是否存在
        if (dbUser == null) {
            log.info("用户不存在");
            throw new BusinessException("用户不存在");
        }
        //校验密码

        String testPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!testPassword.equals(dbUser.getPassword())) {
            log.info("密码错误");
            throw new BusinessException("密码错误");
        }
        //校验用户的状态
        if (dbUser.getStatus() == 0) {
            log.info("用户被禁用");
          throw new BusinessException("用户被停用");
        }
        //封装数据
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(dbUser.getId());
        loginResultVo.setUsername(dbUser.getUsername());
        loginResultVo.setName(dbUser.getName());
        loginResultVo.setImage(dbUser.getImage());
        loginResultVo.setRoleLabel(dbUser.getRoleLabel());
        //生成令牌
       Map<String, Object> claims = new HashMap<>();
        claims.put("id", dbUser.getId());
        claims.put("username", dbUser.getUsername());
        String s = JwtUtils.generateToken(claims);
        //设置令牌
        loginResultVo.setToken(s);
        return loginResultVo;

    }
}
